﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Saker.Utility;
using Saker.Kernel.Model;
using Saker.Kernel.BLL;

namespace Saker.Web.Boss.Sys.Modules
{
    /// <summary>
    /// AuthorityAdd1 的摘要说明
    /// </summary>
    public class AuthorityAdd1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "application/json";
            string cmd = context.Request["CMD"].ToString();
            if (cmd == "add")
            {
                SysAuthorityDir authority = new SysAuthorityDir();
                authority.AuthorityName = context.Request["name"].ToString();
                authority.AuthorityTag = context.Request["tag"].ToString();
                authority.AuthorityDescription = context.Request["des"].ToString();
                authority.AuthorityOrder =int.Parse(context.Request["order"].ToString());
                if (new SysAuthorityDirBLL().Add(authority)!=null)
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 1, message = "添加权限成功！" }));
                }
                else
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 0, message = "添加权限失败！" }));
                }
            }
            if (cmd == "get")
            {
                string id = context.Request["id"].ToString();
                SysAuthorityDir authority = new SysAuthorityDirBLL().GetByAuthorityID(int.Parse(id));
                context.Response.Write(JsonHelper.objectToJson(authority));
            }
            if (cmd == "edit")
            {
                string id = context.Request["id"].ToString();
                SysAuthorityDir authority = new SysAuthorityDirBLL().GetByAuthorityID(int.Parse(id));
                authority.AuthorityName = context.Request["name"].ToString();
                authority.AuthorityTag = context.Request["tag"].ToString();
                authority.AuthorityDescription = context.Request["des"].ToString();
                authority.AuthorityOrder = int.Parse(context.Request["order"].ToString());
                if (new SysAuthorityDirBLL().Update(authority)>0)
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 1, message = "修改权限成功！" }));
                }
                else
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 0, message = "修改权限失败！" }));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}